﻿using Microsoft.IdentityModel.Tokens;
using S.Weaning.Domain;
using S.Weaning.Infrastructure.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace S.Weaning.Infrastructure.Impl
{
    public class WeaningRepository : BaseRepository<MaternalWeaningModel>, IWeaningRepository
    {
        private readonly MyDbContext db;

        public WeaningRepository(MyDbContext db) : base(db)
        {
            this.db = db;
        }
        /// <summary>
        /// 批量添加种母断奶信息
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        public int AddMaternalWeaning(List<MaternalWeaningModel> m)
        {
            db.MaternalWeaningModel.AddRange(m);
            int rowsAffected = db.SaveChanges();

            return rowsAffected;
        }
        /// <summary>
        /// 种母断奶显示
        /// </summary>
        /// <param name="ddate"></param>
        /// <param name="ear"></param>
        /// <param name="build"></param>
        /// <param name="userid"></param>
        /// <returns></returns>
        public List<MaternalWeaningModel> ShowMaternalWeaning(string? ddate, string? ear,string? build,int userid)
        {
            var list = db.MaternalWeaningModel.AsQueryable();
            if (!string.IsNullOrEmpty(ddate))
            {
                DateTime searchDate = DateTime.Parse(ddate);
                list = list.Where(t => t.WeaningDate.HasValue && t.WeaningDate.Value.Date == searchDate.Date);
            }
            if (!string.IsNullOrEmpty(ear))
            {
                list = list.Where(t => t.EITag.Contains(ear));
            }
            if (!string.IsNullOrEmpty(build))
            {
                list = list.Where(t => t.TransferBuilding == build);
            }
            if (userid!=0)
            {
                list = list.Where(t => t.UserId == userid);
            }
            return list.ToList();
        }
    }
}
